package org.example.reflection;

import org.example.Person;
import org.junit.Test;

import java.lang.annotation.ElementType;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;

/**
 * @version: 1
 * @description: java
 * @author: java
 * @create: 2020-06-04 14:59
 **/
public class testDemo1 {
    public static void main(String[] args) throws Exception {
        //获取Class实例的方式
        ClassLoader classLoader = testDemo1.class.getClassLoader();
//        classLoader.
        Class clazz1 = Dog.class;
        Dog dog22 = new Dog();
        Class clazz = dog22.getClass();
        Class<?> dog = Class.forName("org.example.reflection.Dog");
        //通过反射创建Person类对象
        Constructor<?> constructor = dog.getConstructor(String.class, int.class);
        Object obj = constructor.newInstance("Tom", 222);
        Dog dog1 = (Dog) obj;
        System.out.println(obj.toString());
        //通过反射指定属性和方法
        Field name = dog.getDeclaredField("name");
        name.setAccessible(true);
        name.set(dog1, "doggog");

        System.out.println(dog1.toString());
        //调用方法
        Method show = dog.getDeclaredMethod("show");

        show.invoke(dog1);
    }

    @Test
    public void test1() {//除了可以表示类还可以表示如下
        //class实例可以是哪些结构
        Class c1 = Object.class;
        Class c2 = Comparable.class;
        Class c3 = String[].class;
        Class c4 = int[][].class;
        Class c5 = ElementType.class;
        Class c6 = Override.class;
        Class c7 = int.class;
        Class c8 = void.class;
        Class c9 = Class.class;

        int[] a = new int[10];
        int[] b = new int[100];
        Class<? extends int[]> c10 = a.getClass();
        Class<? extends int[]> c11 = b.getClass();
        //只要元素类型和维度一样就是同一个Class
        System.out.println(c11 == c10);//同一个Class

    }
}

